package com.oozol.lieluyun.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.parser.Feature;
import com.oozol.lieluyun.config.AppConfig;
import com.oozol.lieluyun.entity.DockerContainers;
import com.oozol.lieluyun.entity.DockerFilters;
import com.oozol.lieluyun.entity.DockerImages;
import com.oozol.lieluyun.service.IDockerService;
import com.oozol.lieluyun.utils.BeansUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;

@Service
public class DockerServiceImpl implements IDockerService {
   @Autowired
   private AppConfig appConfig;

   public List<DockerImages> getDockerImages(String jobName) throws Exception {
      List<DockerImages> dockerImages = new ArrayList();

      try {
         RestTemplate restTemplate = new RestTemplate();
         String res = null;
         String urlFilters = "/images/json";
         if (!Objects.isNull(jobName)) {
            Map<String, String> map = new HashMap();
            map.put("filters", JSON.toJSONString(new DockerFilters(Arrays.asList(jobName))));
            urlFilters = "/images/json?filters={filters}";
            res = (String)restTemplate.getForObject(this.appConfig.getDockerServerURI() + urlFilters, String.class, map);
         } else {
            res = (String)restTemplate.getForObject(this.appConfig.getDockerServerURI() + urlFilters, String.class, new Object[0]);
         }

         dockerImages = (List)JSON.parseObject(res, new TypeReference<List<DockerImages>>() {
         }, new Feature[0]);
         dockerImages.forEach((item) -> {
            item.setRepository(((String)item.getRepoTags().get(0)).split(":")[0]);
            item.setTag(((String)item.getRepoTags().get(0)).split(":")[1]);
            item.setImageId(item.getId().split(":")[1].substring(0, 12));
            item.setSizeText(BeansUtils.getPrintSize(item.getSize(), 1000));
         });
      } catch (HttpClientErrorException var7) {
         System.out.println("http客户端请求出错了！");
      }

      return dockerImages;
   }

   public List<DockerContainers> getDockerContainers() throws Exception {
      List<DockerContainers> dockerContainers = new ArrayList();

      try {
         RestTemplate restTemplate = new RestTemplate();
         String res = (String)restTemplate.getForObject(this.appConfig.getDockerServerURI() + "/containers/json", String.class, new Object[0]);
         dockerContainers = (List)JSON.parseObject(res, new TypeReference<List<DockerContainers>>() {
         }, new Feature[0]);
         dockerContainers.forEach((item) -> {
            item.setContainersId(item.getId().substring(0, 12));
            if (item.getPorts().size() > 0) {
               DockerContainers.Ports ports = (DockerContainers.Ports)item.getPorts().get(0);
               String port = ports.getIP() + ":" + ports.getPublicPort() + "->" + ports.getPrivatePort() + "/" + ports.getType();
               item.setPortsText(port);
            }

         });
      } catch (HttpClientErrorException var4) {
         System.out.println("http客户端请求出错了！");
      }

      return dockerContainers;
   }
}
